package com.chenfan.mcn.clients.datastore;

import com.alibaba.fastjson.JSONObject;
import com.chenfan.common.exception.BusinessException;
import com.chenfan.mcn.clients.datastore.model.*;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.utils.EncryptUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 数仓服务
 *
 * @author: xuxianbei
 * Date: 2021/8/5
 * Time: 14:35
 * Version:V1.0
 */
@Service
@Slf4j
public class DataStoreServer {

    @Autowired
    private MiniMagicRemoteService miniMagicRemoteService;

    @Value("${data.store.token}")
    private String token;

    private Integer platformsCodeToInt(String code) {
        switch (code) {
            case "weibo":
            case "micro-blog":
                return 2;
            case "xiaohongshu":
            case "red_book":
                return 3;
            case "bilibili":
            case "Station_B":
                return 4;
            case "Tiktok":
            case "douyin":
                return 5;
            case "Kwai_Fu":
            case "kuaishou":
                return 6;
            default:
                throw new BusinessException(McnErrorCode.DATA_UNDEFINE);
        }
    }


    /**
     * 获取MCN粉丝数
     *
     * @param code
     * @param platformUserId
     * @return
     */
    public List<PlatformFansItem> getDcMcnFans(String code, String platformUserId) {
        if (StringUtils.isEmpty(platformUserId)) {
            return new ArrayList<>();
        }
        DataStoreResponse dataStoreResponse = miniMagicRemoteService.getDcMcnFans(platformsCodeToInt(code), platformUserId, "DC", token);
        if (vertifyDate(dataStoreResponse)) {
            PlatformFansInfo platformFansInfo = decode(dataStoreResponse.getEncryptData(), PlatformFansInfo.class);
            return platformFansInfo.getList();
        }
        return new ArrayList<>();
    }

    /**
     * 获取红人指标
     *
     * @param code
     * @param platformUserId
     * @return
     */
    public List<RedQuotaItem> getDcMcnQuota(String code, String platformUserId) {
        if (StringUtils.isEmpty(platformUserId)) {
            return new ArrayList<>();
        }
        DataStoreResponse dataStoreResponse = miniMagicRemoteService.getDcMcnQuota(platformsCodeToInt(code), platformUserId, "DC", token);
        if (vertifyDate(dataStoreResponse)) {
            RedQuotaInfo redQuotaInfo = decode(dataStoreResponse.getEncryptData(), RedQuotaInfo.class);
            return redQuotaInfo.getList();
        }
        return new ArrayList<>();
    }

    private boolean vertifyDate(DataStoreResponse dataStoreResponse) {
        return dataStoreResponse != null && dataStoreResponse.getCode() == 0 && StringUtils.isNotBlank(dataStoreResponse.getEncryptData());
    }

    /**
     * 解码
     *
     * @param value
     * @return
     */
    public <T> T decode(String value, Class<T> clazz) {
        String decrypt = EncryptUtil.decrypt(value, "1135c369b6d5491b88b55904ea838654");
        log.info(decrypt);
        return JSONObject.parseObject(decrypt, clazz);
    }

}
